#!usr/bin/perl
use Data::Dumper; 

sub arrange_class {
	my $tcher_info_time = $_[0];
	my $tcher_info_fre = $_[1];
	my $timetbl = $_[2];
	
  my @courses = keys %$tcher_info_time;
  my %course_times;
  my %courses_selnums;
	foreach my $course (@courses) {
		
		my $fre = $tcher_info_fre->{$course};
    my $time = $tcher_info_time->{$course};
		my @sel_num = &virtual_class($time);
    $course_times->{$course} = &comp_course_time($fre,\@sel_num);
    my @result;
    my @c_selnums;
    &comp_course_selnum(\@c_selnums,$fre,\@sel_num,\@result);
    $courses_selnums->{$course} = \@result;
	}
  my %selnums_course;
  my $fre_count = &comp_fre_count($tcher_info_fre,\@courses);
  &merge_courses_selnums(0,$courses_selnums,\%selnums_course,\@$timetbl,$fre_count);
  \@$timetbl;
}


sub comp_fre_count {
  my $tcher_fre = @_[0];
  my $courses   = @_[1];
  
  my $count;
  foreach my $course (@$courses) {
    my $fre = $tcher_fre->{$course};
    $count = $count + $fre;
  }
  $count;
}


sub merge_courses_selnums {

  my $index = $_[0];
  my $courses_selnums = $_[1];
  my $selnums_course = $_[2];
  my $selnums_courses = $_[3];
  my $fre_count = $_[4];

  my $course = (keys %$courses_selnums)[$index];
  my $len =scalar(keys %$courses_selnums);
  my %c_selnums_course = %$selnums_course;

  $index++;
  if ($index > $len) {
    my $c_selnums_course_key_num = scalar (keys %c_selnums_course);
    if ($c_selnums_course_key_num == $fre_count) {
      push (@$selnums_courses,\%c_selnums_course);
    }
  }
  else {
    my $course_selnums = $courses_selnums->{$course};
    foreach my $selnums (@$course_selnums) {
      if (scalar @$selnums_courses >= 10) {
        return;
      }
      %c_selnums_course = %$selnums_course;
      foreach my $selnum (@$selnums) {
        $c_selnums_course{$selnum} = $course;
      }
      &merge_courses_selnums($index,$courses_selnums,\%c_selnums_course,\@$selnums_courses,$fre_count);
    }
  }
}



sub comp_course_selnum {
  my $c_selnums = $_[0];
  my $c_course_time = $_[1];
  my $all_selnums = $_[2];
  my $result = $_[3];

  my $num = 0;
  $c_course_time--;
  
  foreach my $selnum (@$all_selnums) {
    my @c_f_selnums = @$c_selnums;
    push (@c_f_selnums,$selnum);
    my @all_selnums_new = @$all_selnums;
    if (scalar @$all_selnums_new == $num+1) {
      return;
    }
    splice @all_selnums_new, 0, $num+1;
    $num++;
    if ($c_course_time == 0) {
      push (@$result,\@c_f_selnums);
    }
    else {
      &comp_course_selnum(\@c_f_selnums,$c_course_time,\@all_selnums_new,\@$result);
    }
  }
}

sub comp_course_time {
  my $fre = $_[0];
  my $sel_nums = $_[1];

  my $sel_num = scalar @$sel_nums;
  print Dumper($fre);
  print Dumper($sel_num);


  my $course_time = 1;
  for (my $i=1 ;$i<=$fre;$i++) {
    $course_time = $course_time * $sel_num;
    $sel_num--;
  }
  for ($i=1 ;$i<=$fre;$i++) {
    $course_time = $course_time / $i;
  }
  #print Dumper($course_time);
  $course_time;
}


sub virtual_class {
	my $time = $_[0];
	my @sel_num;	

	@sel_num = &virtual_class_day($time->{1},\@sel_num,"1");
  @sel_num = &virtual_class_day($time->{2},\@sel_num,"2");
  @sel_num = &virtual_class_day($time->{3},\@sel_num,"3");
  @sel_num = &virtual_class_day($time->{4},\@sel_num,"4");
  @sel_num = &virtual_class_day($time->{5},\@sel_num,"5");
  @sel_num;
}	


sub virtual_class_day {
	my $time_range = $_[0];
	my $sel_num = $_[1];
	my $week = $_[2];
	
  #print Dumper($time_range);
  my $st = $time_range->{st_time};
  my $ed = $time_range->{ed_time};
  # print "$st;$ed;\n";
  #if (($time_range->{st_time} le "800")&&($time_range->{ed_time} ge "930")) {
	if (( $st <= 800)&&( $ed >= 930)) {
		my $num = $week . "1";
		push (@$sel_num,$num);
    #print "1\n";
	}
        if (($st <= 1000)&&($ed >= 1130)) {
                my $num = $week . "2";
                push (@$sel_num,$num);
        }
	        if (($st <= 1330)&&($ed >= 1500)) {
                my $num = $week . "3";
                push (@$sel_num,$num);
        }
	        if (($st <= 1530)&&($ed >= 1700)) {
                my $num = $week . "4";
                push (@$sel_num, $num);
        }
	        if (($st <= 1900)&&($ed >= 2030)) {
                my $num = $week . "5";
                push (@$sel_num, $num);
        }
        #print Dumper(\@$sel_num);
	@$sel_num;
}

1
